package com.lhkj.ct.meta.modules.psychology.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhkj.ct.base.model.dto.PageQuery;
import com.lhkj.ct.meta.modules.psychology.model.bo.AllotRecordBo;
import com.lhkj.ct.meta.modules.psychology.model.entity.TblScalePsyRecord;
import com.lhkj.ct.meta.modules.psychology.model.vo.PsyRecordsVo;
import com.lhkj.ct.meta.modules.psychology.model.vo.ViewAnswerVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *      测验记录 Mapper 接口
 * </p>
 *
 * @author jobob
 * @since 2023-10-12
 */
public interface PsyRecordMapper extends BaseMapper<TblScalePsyRecord> {

    /**
     * 分配记录 - 分页查询
     */
    IPage<PsyRecordsVo> selectPages(Page<TblScalePsyRecord> page,@Param("record") PsyRecordsVo psyRecord,@Param("pageQuery") PageQuery pageQuery);

    /**
     * 根据用户查询分配记录
     */
    IPage<PsyRecordsVo> selectPagesByPatient(Page<TblScalePsyRecord> page,@Param("record") PsyRecordsVo psyRecord,@Param("pageQuery") PageQuery pageQuery);

    /**
     * 作答时查询
     * @param allotId 分发记录id
     * @return  分发记录id， 开始时间，截止时间， 报告id，报告状态， 患者信息
     */
    AllotRecordBo selectRecordWithRid(@Param("allotId") String allotId);

    /**
     * 查看作答结果
     */
    ViewAnswerVo viewAnswer(@Param("allotId") String allotId);

    @Select("select count(t.id) from tbl_scale_psy_record t left join tbl_scale_report t1 on t1.allot_id = t.id " +
            "where t.pat_id = #{patId,jdbcType=VARCHAR} and t.del_flag = ${@com.lhkj.ct.base.enums.DeleteStatus@NORMAL.code} and (t1.report_status is null or t1.report_status = -1)")
    Long countNotDoneByPatient(@Param("patId") String patId);

    /**
     * 查询日期区间按天统计
     * @param start 从start开始， 包括start
     * @param end   到end结束，不包括end
     * @return
     */
    List<Map<String, Object>> countGroupByDate(@Param("start") LocalDate start, @Param("end") LocalDate end);
}
